package cn.dglydrpy.study.algorithm.od;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author Yyy
 * @date 2024/11/5 12:19
 * @description
 * 题目描述：现有两组服务器 A 和 B，每组有多个算力不同的 CPU。
 * 为让两组服务器算力相等，允许从每组各选一个 CPU 进行一次交换，
 * 求两组服务器中用于交换的 CPU 的算力，且从 A 组服务器中选出的 CPU 算力尽可能小。
 */
public class Od0002_CPUCalculation {

    public static void main(String[] args) {
        
//        int size1 = 1;
//        int size2 = 2;
//        int[] a = new int[]{2};
//        int[] b = new int[] {1,3};

        // 交互式验证
        Scanner in = new Scanner(System.in);
        while (in.hasNextLine()) {
            String cpuGroup = in.nextLine();
            String aCPUStr = in.nextLine();
            String bCPUStr = in.nextLine();
            int[] a = new int[Integer.valueOf(cpuGroup.split(" ")[0])];
            int[] b = new int[Integer.valueOf(cpuGroup.split(" ")[1])];

            String[] s1 = aCPUStr.split(" ");
            for (int i = 0; i < s1.length; i++) {
                Integer integer = Integer.valueOf(s1[i]);
                a[i] = integer;
            }
            String[] s2 = bCPUStr.split(" ");
            for (int i = 0; i < s2.length; i++) {
                Integer integer = Integer.valueOf(s2[i]);
                b[i] = integer;
            }
            
            
            int size1 = Integer.valueOf(cpuGroup.split(" ")[0]);
            int size2 = Integer.valueOf(cpuGroup.split(" ")[1]);
            
            String myAnswer = myAnswer(size1, size2, a, b);
            System.out.println(myAnswer);
            String answer1 = answer1(size1, size2, a, b);
            System.out.println(answer1);
            if(!myAnswer.equals(answer1)){
                System.out.println("Fucking fucked!");
            }
            
        }
    }
    
    private static String answer1(int size1,int size2,int[] a,int[] b){
        
        int totalPowerA = 0;
        for (int i = 0; i < a.length; i++) {
            totalPowerA += a[i];
        }
        Arrays.sort(a);
       
        int totalPowerB = 0;
        for (int i = 0; i < b.length; i++) {
            totalPowerB += b[i];
        }
        Arrays.sort(b);
        
        int diff = (totalPowerA - totalPowerB) / 2;
        
        for (int i = 0; i < a.length; i++) {
                int target = a[i] - diff;
                if (Arrays.binarySearch(b, target) >= 0) {
                    return(a[i] + " " + target);
                }
        }
        return "";
    }
    
    private static String myAnswer(int size1,int size2,int[] a,int[] b){
        String ans = "";
        
        int sum1 = 0;
        for(int i : a){
            sum1 += i;
        }
        
        int sum2 = 0;
        for(int j : b){
            sum2 += j;
        }
        
        // a组排顺序
        Arrays.sort(a);
        int diff = 0;
        // a组初始算力高
        if(sum1 > sum2){
            diff = (sum1 - sum2)/2;
            System.out.println(diff);
            for(int i : a){
                for(int j : b){
                    if(i - j == diff && sum1 - diff == sum2 + diff){
                        ans = ans + i + " " + j;
                        return ans;
                    }
                }
            }
            
            
        }else{
        // b组初始算力高
            diff = (sum2 - sum1)/2;
            for(int i : a){
                for(int j : b){
                    if(j - i == diff && sum1 + diff == sum2 - diff){
                        ans = ans + i + " " + j;
                        return ans;
                    }
                }
            }
            
        }
        
        return ans;
    }
}
